﻿$(function () {
    toastr.options.positionClass = 'toast-bottom-center';
    var vm = new Vue({
        el: '#app',
        data: {
            // 角色与权限
            applyDepartmentPermission: false,
            createPermission: false,
            deletePermission: false,
            editPermission: false,
            syncPermission:false,


            // 主页面
            filterName: '',
            dialogVisible: false,
            dialogTitle: '新建申请科室',
            actionType: '创建',
            tableData: [],
            formLabelWidth: '80px',

            selectedApplyDepartments: [],

            // 当前申请科室
            applyDepartment: {
                id: null,
                name: null,
                code: null,
                institutionCode:null,
                outerCode: null,
                isActive: true
            },

            defaultProps: {     // 科室和tree-node对象的映射
                label: 'name',
                children: 'children'
            },

            // 验证规则
            rules: {
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' }
                ],
                outerCode: [
                    { required: true, message: '请输入外部编号', trigger: 'blur' }
                ]
            }
        },
        methods: {
            // 加载当前用户角色和权限.
            loadPermission() {
                var vm = this;
                return new Promise((resolve, reject) => {
                    if (abp.auth.isGranted('BaseSettingGroup.ApplyDepartments')) { // 设备类型页面权限
                        vm.applyDepartmentPermission = true;
                        if (abp.auth.isGranted('BaseSettingGroup.ApplyDepartments.Add')) {// 创建权限
                            vm.createPermission = true;
                        }
                        if (abp.auth.isGranted('BaseSettingGroup.ApplyDepartments.Delete')) { // 删除权限
                            vm.deletePermission = true;
                        }
                        if (abp.auth.isGranted('BaseSettingGroup.ApplyDepartments.Edit')) { // 编辑权限
                            vm.editPermission = true;
                        }
                        if (abp.auth.isGranted('BaseSettingGroup.ApplyDepartments.Sync')) { // 同步权限
                            vm.syncPermission = true;
                        }
                    }
                    resolve();
                });
            },
            // 加载表格数据
            loadTableData() {
                var vm = this;
                abp.ajax({
                    type: 'get',
                    url: `/api/apply-department/get-list?filterName=${vm.filterName}`
                }).then((res) => {
                    if (res.code === 0) {
                        vm.tableData = res.data;
                    }
                    else {
                        abp.message.error(res.msg);
                    }
                });
            },

            // 加载设备类型详情
            getApplyDepartmentDetail(id) {
                var vm = this;
                abp.ajax({
                    type: 'get',
                    url: `/api/apply-department/get-detail?id=${id}`
                }).then(res => {
                    if (res.code === 0) {
                        vm.applyDepartment = res.data;
                    }
                    else {
                        vm.dialogVisible = false;
                        abp.message.error(res.msg);
                    }
                });
            },

            // 选择改变
            selectionChange(val) {
                var vm = this;
                vm.selectedApplyDepartments = val;
            },

            // 删除
            deleteMany() {
                var vm = this;
                var idList = vm.selectedApplyDepartments.map(item => item.id);
                if (idList.length > 0) {
                    abp.message.confirm('删除设备类型，设备类型下的所有设备将被删除。')
                        .then(confirmed => {
                            if (confirmed) {
                                abp.ajax({
                                    type: 'post',
                                    url: '/api/apply-department/delete-many',
                                    data: JSON.stringify(idList)
                                }).then(res => {
                                    if (res.code == 0) {
                                        abp.notify.success(res.msg);
                                        vm.loadTableData();
                                    }
                                })
                            }
                        })
                }
                else {
                    abp.notify.error('未选中任何行！');
                }
            },

            // 删除单个
            deleteApplyDepartment(id) {
                var vm = this;
                abp.message.confirm('确认删除设备类型？')
                    .then(confirmed => {
                        if (confirmed) {
                            abp.ajax({
                                type: 'get',
                                url: `/api/apply-department/delete?id=${id}`
                            }).then(res => {
                                if (res.code === 0) {
                                    abp.notify.success(res.msg);
                                    vm.loadTableData();
                                }
                            })
                        }
                    })
            },

            // 显示创建对话框
            showCreate() {
                var vm = this;
                vm.actionType = '创建';
                vm.dialogTitle = '新建';
                vm.dialogVisible = true;
            },

            // 提交创建
            createSubmit() {
                var vm = this;
                vm.$refs['applyDepartmentForm'].validate((valid) => {
                    if (valid) {
                        abp.ajax({
                            type: 'post',
                            url: `/api/apply-department/submit-create`,
                            data: JSON.stringify(vm.applyDepartment)
                        }).then(res => {
                            if (res.code == 0) {
                                vm.resetForm();
                                abp.notify.success(res.msg);
                                vm.loadTableData();
                            }
                            else {
                                vm.dialogVisible = false;
                                abp.message.error(res.msg).then(() => {
                                    vm.dialogVisible = true;
                                })
                            }
                        });
                    }
                });

            },

            // 显示修改对话框
            showEdit(index, row) {
                var vm = this;
                vm.actionType = '编辑';
                vm.dialogTitle = '编辑';
                vm.dialogVisible = true;
                vm.getApplyDepartmentDetail(row.id);
            },

            // 提交修改
            editSubmit() {
                var vm = this;
                vm.$refs['applyDepartmentForm'].validate((valid) => {
                    if (valid) {
                        abp.ajax({
                            type: 'post',
                            url: '/api/apply-department/submit-edit',
                            data: JSON.stringify(vm.applyDepartment)
                        }).then((res) => {
                            if (res.code === 0) {
                                vm.resetForm();
                                abp.notify.success(res.msg);
                                vm.loadTableData();
                            }
                            else {
                                vm.dialogVisible = false;
                                abp.notify.error(res.msg).then(() => {
                                    vm.dialogVisible = true;
                                });
                            }
                        })
                    }
                });

            },

            // 保存
            save() {
                var vm = this;
                if (vm.actionType === '创建') {
                    vm.createSubmit()
                }
                else {
                    vm.editSubmit();
                }
            },

            // 同步
            syncData() {
                var vm = this;
                abp.message.confirm('从HIS系统同步申请科室数据')
                    .then(confirmed => {
                        if (confirmed) {
                            abp.ajax({
                                type: 'GET',
                                url: '/api/apply-department/sync-data'
                            }).then(res => {
                                if (res.code === 0) {
                                    abp.notify.success(res.msg);
                                    vm.loadTableData();
                                }
                            })
                        }
                    })
            },

            // 表单重置
            resetForm() {
                var vm = this;
                vm.$refs['applyDepartmentForm'].resetFields();
                vm.dialogVisible = false;
                Object.keys(vm.applyDepartment).forEach(key => {
                    vm.applyDepartment[key] = null;
                });
                vm.applyDepartment.isActive = true;
            }


        },
        created() {
            var vm = this;
            vm.loadPermission().then(() => {
                vm.loadTableData();
            })
        },
        mounted() {
        }
    });
});